package com.zhuchang.self_rpc.test.democoding.nettydubbo.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.Callable;

/**
 * @Author RenPu
 * @Date 2022/2/12 15:13
 * @Version 1.0
 * @Description:
 **/
public class NettyDubboClientHandler extends ChannelInboundHandlerAdapter implements Callable {

    private  ChannelHandlerContext channelHandlerContext;  //上下文对象

    private String result;   //响应结果集

    private String para;    //调用客户端方法时，入参值；


    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        this.channelHandlerContext=ctx;
    }

    @Override
    public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("channelRead 被调用........");
        this.result=msg.toString();

        notify();  //唤醒等待线程
    }



    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("异常原因为："+cause.getMessage());
        ctx.close();

    }



    @Override
    public synchronized Object call() throws Exception {

        System.out.println("call1 被调用....");
        channelHandlerContext.writeAndFlush(para);
        wait();

        System.out.println("call2 被调用....");
        return result;
    }


    public void setPara(String providerName){
        System.out.println("setPara .........");
        this.para=providerName;
    }


}
